package com.weijianhuawen.wjblog.admin.controller;

import com.weijianhuawen.wjblog.admin.mode.vo.article.*;
import com.weijianhuawen.wjblog.admin.service.AdminArticleService;
import com.weijianhuawen.wjblog.common.aspect.ApiOperationLog;
import com.weijianhuawen.wjblog.common.utils.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-10-24 17:23
 */

@RestController
@RequestMapping("/admin/article")
@Api(tags = "Admin 文章管理模块")
public class AdminArticleController {
    @Autowired
    private AdminArticleService adminArticleService;

    @PostMapping("/publish")
    @ApiOperation(value = "文章发布")
    @ApiOperationLog(description = "文章发布接口")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Object publishArticle(@RequestBody @Validated PublishArticleReqVo vo) {
        return adminArticleService.publishArticle(vo);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "文章删除")
    @ApiOperationLog(description = "文章删除接口")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Object deleteArticle(@RequestBody @Validated DeleteArticleReqVo vo) {
        return adminArticleService.deleteArticle(vo);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询文章分页数据")
    @ApiOperationLog(description = "查询文章分页数据")
    public Response findArticlePageList(@RequestBody @Validated FindArticlePageListReqVo vo) {
        return adminArticleService.findArticlePageList(vo);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "查询文章详情")
    @ApiOperationLog(description = "查询文章详情")
    public Response findArticleDetail(@RequestBody @Validated FindArticleDetailReqVo vo) {
        return adminArticleService.findArticleDetail(vo);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新文章")
    @ApiOperationLog(description = "更新文章")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response updateArticle(@RequestBody @Validated UpdateArticleReqVo vo) {
        return adminArticleService.updateArticle(vo);
    }


    @PostMapping("/auto/save")
    @ApiOperation(value = "自动保存文章")
    @ApiOperationLog(description = "自动保存文章")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response autoSaveArticle(@RequestBody @Validated AutoSaveArticleReqVo vo) {
        return adminArticleService.autoSaveArticle(vo);
    }

    @PostMapping("/isTop/update")
    @ApiOperation(value = "设置文章置顶状态")
    @ApiOperationLog(description = "文章置顶")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response updateArticleTopState(@RequestBody @Validated UpdateArticleIsTopReqVo vo) {
        return adminArticleService.setArticleTop(vo);
    }
}
